<!--suppress ALL -->
<template>
  <div style="background-color: #fcfcfc">
    <el-dialog :visible.sync="editVisible" width="85%" :close-on-click-modal="true">
      <el-container>
        <el-header height="20px">
          <!-------------------------------按钮 ----------------------down------------------->
          <div style="display: flex">
            <el-date-picker
              style="margin-right: 20px;width: 150px;"
              v-model="query.sddate"
              align="left"
              type="date"
              placeholder="起始日期"
            />
            <el-date-picker
              style="margin-right: 20px;width: 150px;"
              v-model="query.eddate"
              align="right"
              type="date"
              placeholder="结束日期"
            />
            <el-input v-model="query.ccode" class="handle-input" style="width: 150px;" placeholder="单号" />
            <el-input v-model="query.cinvcode" class="handle-input" style="width: 150px;" placeholder="产品编码" />
            <el-input v-model="query.cwhname" class="handle-input" style="width: 150px;" placeholder="仓库" />
            <el-input v-model="query.csocode" class="handle-input" style="width: 150px;" placeholder="需求跟踪号" />
            <el-input v-model="query.cvenname" class="handle-input" style="width: 200px;" placeholder="供应商名称" />

            <el-button class="hand-btn" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            <el-button class="hand-btn" type="primary" icon="el-icon-search" @click="clearQuery">清空</el-button>
          </div>
          <!-------------------------------按钮 ----------------------up ------------------->
        </el-header>
        <el-main>
          <el-table
            v-loading="loading"
            :data="page.content"
            max-height="560"
            height="560"
            :cell-style="{'padding':'2px','font-size':'12px'}"
            :header-cell-style="{'padding':'0px','font-weight':'800'}"
            tooltip-effect="dark"
            border
            style="width: 100%;"
          >
            <el-table-column
              type="index"
              label="项次"
              width="50"
            />
            <el-table-column
              prop="cCode"
              width="100"
              label="单号"
            />
            <el-table-column
              prop="dDate"
              width="90px"
              label="日期"
            />
            <el-table-column
              prop="iQuantity"
              width="80px"
              label="数量"
            />
            <el-table-column
              prop="cBDefine13"
              width="70px"
              label="状态"
            >
              <template slot-scope="scope">
                {{ scope.row.cBDefine13?scope.row.cBDefine13:'待质检' }}
              </template>
            </el-table-column>
            <el-table-column
              prop="cInvCode"
              width="120"
              label="产品编码"
            />
            <el-table-column
              width="120"
              prop="cVenName"
              label="供应商名称"
            />
            <el-table-column
              width="120px"
              prop="cWhName"
              label="仓库名称"
            />
            <el-table-column
              width="300px"
              prop="cInvName"
              label="产品名称"
            />
            <el-table-column
              width="220px"
              prop="cInvStd"
              label="产品规格"
            />
            <el-table-column
              width="220px"
              prop="cFree1"
              label="颜色"
            />
            <el-table-column
              width="120px"
              prop="cSOCode"
              label="需求跟踪号"
            />
            <el-table-column
              width="120px"
              prop="csoinvname"
              label="产品型号"
            />
            <el-table-column label="操作" width="140" align="center"  fixed="right">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  icon="el-icon-edit"
                  @click="selTable(scope.row)"
                >质检</el-button>
                <el-button
                  type="text"
                  icon="el-icon-delete"
                  class="red"
                  @click="handlefinish(scope.$index, scope.row)"
                >免检</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-main>
        <el-footer>
          <div class="pagination">
            <el-pagination
              background
              layout="total,sizes, prev, pager, next"
              :page-sizes="[10,20, 30, 40, 50]"
              :current-page="query.pageIndex"
              :page-size="query.pageSize"
              :total="page.totalElements"
              @size-change="handleSizeChange"
              @current-change="handlePageChange"
            />
          </div>
        </el-footer>
      </el-container>
    </el-dialog>

    <el-card :body-style="{padding:'10px'}" style="min-height: 650px;background-color: #f3f3f3"   >
      <!------------------------------按钮 ----------------------up ------------------->
      <el-row :gutter="24" >
        <el-col :span="21">
          <el-button type="primary" v-show="!quality.id" @click="editVisible = true">选择单据</el-button>
          <el-button type="warning" v-show="quality.id"  @click="copyBill">复制</el-button>
          <el-button type="success" @click="saveEdit">保存</el-button>
        </el-col>
        <el-col :span="3">
          <el-button-group>
            <el-button type="primary" icon="el-icon-arrow-left" @click="toLast" />
            <el-button type="primary" icon="el-icon-arrow-right" @click="toNext" />
          </el-button-group>
        </el-col>
      </el-row>
      <!-------------------------------按钮 ----------------------up ------------------->

      <div>
        <el-row style="background-color: #fefefe">
          <el-col :span="15">
            <el-descriptions border style="margin-bottom: 5px">
              <el-descriptions-item label="方案名称" label-style="width:8%" content-style="width:20%">{{ quality.projectName }}</el-descriptions-item>
              <el-descriptions-item label="备注" :span="2" label-style="width:8%" content-style="width:23%">{{ quality.remark }}</el-descriptions-item>
              <el-descriptions-item label="来料单号" label-style="width:8%" content-style="width:23%">
                <div style="text-align: left;color: #81a4ff;font-weight: bold;font-size: 18px">{{ quality.ccode }}</div><div style="float: right;font-size: 15px">{{quality.ddate}}</div>
              </el-descriptions-item>
              <el-descriptions-item label="供应商名称" label-style="width:8%" content-style="width:23%">{{ quality.cvenname }}</el-descriptions-item>
              <el-descriptions-item label="仓库名称" label-style="width:8%" content-style="width:23%">{{quality.cwhname}}</el-descriptions-item>
              <el-descriptions-item label="产品编码" label-style="width:8%" content-style="width:23%">{{ quality.cinvcode }}</el-descriptions-item>
              <el-descriptions-item label="产品名称" label-style="width:8%" content-style="width:23%">{{ quality.cinvname }}</el-descriptions-item>
              <el-descriptions-item label="产品规格" label-style="width:8%" content-style="width:23%">{{ quality.cinvstd }}</el-descriptions-item>
              <el-descriptions-item label="销售单号" label-style="width:8%" content-style="width:23%">{{quality.csocode}}</el-descriptions-item>
              <el-descriptions-item label="应用机型" label-style="width:8%" content-style="width:23%">{{quality.csoinvname}}</el-descriptions-item>
              <el-descriptions-item label="物料类别" label-style="width:8%" content-style="width:23%">
                <el-select v-model="quality.productType" allow-create
                           filterable placeholder="请选择" >
                  <el-option
                    v-for="item in this.product_types"
                    :key="item"
                    :label="item"
                    :value="item"
                  />
                </el-select>
              </el-descriptions-item>
              <el-descriptions-item label="质检等级" label-style="width:8%" content-style="width:23%">{{ quality.level }}</el-descriptions-item>
              <el-descriptions-item label="数量/抽检数" label-style="width:8%" content-style="width:23%">{{ quality.qty }}/
                <el-input-number v-model="quality.checkqty"  style="width: 100px" :min="0" :controls="false" @change="changeDemo"/>
              </el-descriptions-item>
              <el-descriptions-item label="版本" label-style="width:8%" content-style="width:23%">
                <el-select v-model="quality.version"  placeholder="请选择" @change="selVersion">
                  <el-option
                    v-for="item in this.projects"
                    :key="item.version"
                    :label="item.version"
                    :value="item.version"
                  />
                </el-select>
              </el-descriptions-item>
            </el-descriptions>
          </el-col>
          <el-col :span="3">
            <div style="text-align: center">AQL值</div>
            <el-descriptions border style="margin-bottom: 5px" :column="1">
              <el-descriptions-item label="A" label-style="width:8%" content-style="width:23%">
                <el-select v-model="quality.aqla" placeholder="请选择" @change="changeAqla">
                  <el-option
                    v-for="item in grades"
                    :key="item"
                    :label="item"
                    :value="item"
                  />
                </el-select>
              </el-descriptions-item>
              <el-descriptions-item label="B" label-style="width:8%" content-style="width:23%">
                <el-select v-model="quality.aqlb" placeholder="请选择" @change="changeAqlb">
                  <el-option
                    v-for="item in grades"
                    :key="item"
                    :label="item"
                    :value="item"
                  />
                </el-select>
              </el-descriptions-item>
              <el-descriptions-item label="C" label-style="width:8%" content-style="width:23%">
                <el-select v-model="quality.aqlc" placeholder="请选择" @change="changeAqlc">
                  <el-option
                    v-for="item in grades"
                    :key="item"
                    :label="item"
                    :value="item"
                  />
                </el-select>
              </el-descriptions-item>
            </el-descriptions>
          </el-col>
          <el-col :span="3">
            <div style="text-align: center">AC值</div>
            <el-descriptions border style="margin-bottom: 5px" :column="1">
              <el-descriptions-item label="AC/A" label-style="width:8%" content-style="width:23%">
                {{quality.aca}}
              </el-descriptions-item>
              <el-descriptions-item label="AC/B" label-style="width:8%" content-style="width:23%">
                {{quality.acb}}
              </el-descriptions-item>
              <el-descriptions-item label="AC/C" label-style="width:8%" content-style="width:23%">
                {{quality.acc}}
              </el-descriptions-item>
            </el-descriptions>
          </el-col>
          <el-col :span="3">
            <div style="text-align: center">RE值</div>
            <el-descriptions border style="margin-bottom: 5px" :column="1">
              <el-descriptions-item label="RE/A" label-style="width:8%" content-style="width:23%">
                {{quality.rea}}
              </el-descriptions-item>
              <el-descriptions-item label="RE/B" label-style="width:8%" content-style="width:23%">
                {{quality.reb}}
              </el-descriptions-item>
              <el-descriptions-item label="RE/C" label-style="width:8%" content-style="width:23%">
                {{quality.rec}}
              </el-descriptions-item>
            </el-descriptions>
          </el-col>
        </el-row>
        <!-------------------------------按钮 ----------------------up ------------------->
        <el-tabs v-model="activeName" type="border-card">
          <el-tab-pane label="质检内容" name="质检内容">

            <el-table
              :data="quality.entitys"
              tooltip-effect="dark"
              border
              style="width: 100%;"
            >
              <el-table-column
                type="index"
                label="项次"
                width="50"
              />
              <el-table-column
                prop="quotaName"
                width="150px"
                label="测试指标"
              />
              <el-table-column
                prop="planName"
                width="150px"
                label="测试项目"
              />
              <el-table-column
                prop="specs"
                width="150px"
                label="规格"
              />
              <el-table-column
                prop="aql"
                width="60px"
                label="AQL参考"
              />
              <el-table-column
                prop="no1"
                width="120px"
                label="NO.1"
              >
                <template slot-scope="scope">
                  <el-select v-if="scope.row.type === '字符'" v-model="scope.row.no1" :disabled="scope.row.qc<1" placeholder="请选择" >
                    <el-option
                      v-for="item in ['符合', '不符合']"
                      :key="item"
                      :label="item"
                      :value="item"
                    />
                  </el-select>
                  <el-input-number v-if="scope.row.type === '数值'" v-model="scope.row.no1" style="width: 100px" :min="0" :controls="false" :disabled="scope.row.qc<1" @blur="check"/>
                </template>
              </el-table-column>
              <el-table-column
                prop="no2"
                width="120px"
                label="NO.2"
              >
                <template slot-scope="scope">
                  <el-select v-if="scope.row.type === '字符'" v-model="scope.row.no2" :disabled="scope.row.qc<2" placeholder="请选择" >
                    <el-option
                      v-for="item in ['符合', '不符合']"
                      :key="item"
                      :label="item"
                      :value="item"
                    />
                  </el-select>
                  <el-input-number v-if="scope.row.type === '数值'" v-model="scope.row.no2" style="width: 100px" :min="0" :controls="false" :disabled="scope.row.qc<2"   @blur="check"/>
                </template>
              </el-table-column>
              <el-table-column
                prop="no3"
                width="120px"
                label="NO.3"
              >
                <template slot-scope="scope">
                  <el-select v-if="scope.row.type === '字符'" v-model="scope.row.no3" :disabled="scope.row.qc<3" placeholder="请选择" >
                    <el-option
                      v-for="item in ['符合', '不符合']"
                      :key="item"
                      :label="item"
                      :value="item"
                    />
                  </el-select>
                  <el-input-number v-if="scope.row.type === '数值'" v-model="scope.row.no3" style="width: 100px" :min="0" :controls="false" :disabled="scope.row.qc<3"   @blur="check"/>
                </template>
              </el-table-column>
              <el-table-column
                prop="no4"
                width="120px"
                label="NO.4"
              >
                <template slot-scope="scope">
                  <el-select v-if="scope.row.type === '字符'" v-model="scope.row.no4" :disabled="scope.row.qc<4" placeholder="请选择" >
                    <el-option
                      v-for="item in ['符合', '不符合']"
                      :key="item"
                      :label="item"
                      :value="item"
                    />
                  </el-select>
                  <el-input-number v-if="scope.row.type === '数值'" v-model="scope.row.no4" style="width: 100px" :min="0" :controls="false" :disabled="scope.row.qc<4"  @blur="check"/>
                </template>
              </el-table-column>
              <el-table-column
                prop="A"
                width="70px"
                label="A"
              >
                <template slot-scope="scope">
                  <el-input-number  :disabled="scope.row.aql != 'A'"  v-model="scope.row.pda" style="width: 50px" :min="0" :controls="false" :precision="0" @change="panduan" />
                </template>
              </el-table-column>
              <el-table-column
                prop="B"
                width="70px"
                label="B"
              >
                <template slot-scope="scope">
                  <el-input-number :disabled="scope.row.aql != 'B'" v-model="scope.row.pdb" style="width: 50px" :min="0" :controls="false" :precision="0" @change="panduan" />
                </template>
              </el-table-column>
              <el-table-column
                prop="C"
                width="70px"
                label="C"
              >
                <template slot-scope="scope">
                  <el-input-number :disabled="scope.row.aql != 'C'" v-model="scope.row.pdc" style="width: 50px" :min="0" :controls="false" :precision="0" @change="panduan" />
                </template>
              </el-table-column>
              <el-table-column
                prop="judge"
                width="90px"
                label="系统"

              />
              <el-table-column
                prop="result"
                width="110px"
                label="人工"
              >
                <template slot-scope="scope">
                  <el-select v-model="scope.row.result" placeholder="请选择" >
                    <el-option
                      v-for="item in ['合格', '不合格']"
                      :key="item"
                      :label="item"
                      :value="item"
                    />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column
                prop="remark"
                width="320px"
                label="备注"
              >
                <template slot-scope="scope">
                  <el-input v-model="scope.row.remark" />
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="图片上传" name="图片上传">
            <el-upload
              :action="fileUploadURL"
              list-type="picture-card"
              :on-success="handleUploadSuccess"
              :on-preview="handlePictureCardPreview"
              :on-exceed="handleExceed"
              :limit="6"
              :file-list="filList"
              :on-remove="handleRemove"
            >
              <i class="el-icon-plus" />
            </el-upload>
            <el-dialog :visible.sync="dialogVisible">
              <img width="100%" :src="dialogImageUrl" alt="">
            </el-dialog>
          </el-tab-pane>
        </el-tabs>
      </div>
      <el-card>
        <el-descriptions border>
          <el-descriptions-item label="备注">
            <el-input v-model="quality.describe" >
            </el-input>
          </el-descriptions-item>
          <el-descriptions-item label="总检" label-style="width:2%" content-style="width:20%">
            <template>
              <el-radio-group v-model="quality.measurement">
                <el-radio label="合格">合格</el-radio>
                <el-radio label="不合格">不合格</el-radio>
              </el-radio-group>
            </template>
          </el-descriptions-item>
        </el-descriptions>
      </el-card>
    </el-card>
  </div>
</template>
<script>
import { save } from '@/api/quality'
import { query as u8query } from '@/api/income'
import { getByCInvCode } from '@/api/project'
import { get as getQuality } from '@/api/quality'
import { get } from '@/api/qmilcode'
import { findDemoByLevelAndQty } from '@/api/qmilcode'
import { finish } from '@/api/income'
import { getNext } from '@/api/quality'
import { getLast } from '@/api/quality'
export default {
  name: 'QualityEdit',
  data() {
    return {
      loading:false,
      filList: [],
      activeName: '质检内容',
      fileURL: process.env.VUE_APP_BASE_API,
      fileUploadURL: process.env.VUE_APP_BASE_API + '/file/upload',
      file: '',
      dialogVisible: false,
      dialogImageUrl: '',
      active: 1,
      grades: [0, 0.01,0.015,0.025,0.04,0.065,0.1,0.25, 0.4, 0.65, 1.0, 1.5, 2.5, 4.0, 6.5, 10, 15, 25,65,100,150,250,650,1000],
      product_types:['综合', '电子', '包材', '电池', '消耗'],
      editVisible: false,
      quality: { qc: 5, entitys: [{}, {}, {}, {}, {}], measurement: '合格' },
      query: {
        sddate:new Date(),
        eddate:new Date()
      },
      page: {},
      form: {},
      projects: []
    }
  },
  created() {
    console.log(this.$route.query)
    this.handleSearch()
  },
  activated(){
    if (this.$route.query.id) {
      this.getInCome(this.$route.query.id)
    }
  },
  methods: {
    // 获取 easy-mock 的模拟数据
    getInCome(id) {
      this.loading = true
      getQuality(id).then(res => {
        this.loading = false
        this.quality = res.data.data
        if(this.product_types.every((type)=>{
          type != this.quality.productType
        })){
          this.product_types.push(this.quality.productType)
        }
        this.filList = []
        for (const image of this.quality.images) {
          this.filList.push({ url: this.fileURL + image.imgUrl })
        }
        if(this.$route.query.copy){
          this.copyBill()
        }
      })
    },
    copyBill(){
      this.quality.id = null
      this.quality.cvencode = null
      this.quality.cvenname = null
      this.quality.cdepname = null
      this.quality.cdepcode = null
      this.quality.autoid = null
      this.quality.qty = null
      this.quality.ccode = null
      this.quality.csoinvname = null
      this.quality.cinvname = null
      this.quality.cinvstd = null
      this.quality.aql = null
      this.quality.projectId = null
      this.quality.projectDescribe = null
      this.quality.projectName = null
      this.quality.zjStatus = null
      this.$message({
        showClose: true,
        message: '复制成功',
        type: 'success'
      })
      this.editVisible = true
      this.setChoujian();
    },
    handleRemove(file, fileList) {
      const images = []
      for (const image of fileList) {
        if (image.response) {
          images.push({ imgUrl: image.response.data.data })
        } else {
          images.push({ imgUrl: image.url.replace(this.fileURL, '') })
        }
      }
      this.quality.images = images
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleExceed() {
      this.$message({
        showClose: true,
        message: '超出限定图片上传个数',
        type: 'error'
      })
    },
    handleUploadSuccess(res, file, fileList) {
      const images = []
      for (const image of fileList) {
        if (image.response) {
          images.push({ imgUrl: image.response.data.data })
        } else {
          images.push({ imgUrl: image.url.replace(this.fileURL, '') })
        }
      }
      this.quality.images = images
    },
    panduan(){
      for (const row of this.quality.entitys) {
        if (row.type === '字符') {
          row.judge = '合格'
          if(row.pda){
            row.pdb  = undefined
            row.pdc  = undefined
            if(this.quality.rea){
              if(row.pda > this.quality.aca){
                row.result = row.judge = '不合格'
                continue;
              }
            }else {
              this.$message({
                showClose: true,
                message: '请选择AQL值A',
                type: 'warning'
              })
              return;
            }
          }
          if(row.pdb){
            row.pda  = undefined
            row.pdc  = undefined
            if(this.quality.reb){
              if(row.pdb > this.quality.acb){
                row.result = row.judge = '不合格'
                continue;
              }
            }else {
              this.$message({
                showClose: true,
                message: '请选择AQL值B',
                type: 'warning'
              })
              return;
            }
          }
          if(row.pdc){
            row.pdb  = undefined
            row.pda  = undefined
            if(this.quality.rec){
              if(row.pdc > this.quality.acc){
                row.result = row.judge = '不合格'
                continue;
              }
            }else {
              this.$message({
                showClose: true,
                message: '请选择AQL值C',
                type: 'warning'
              })
              return;
            }
          }
          row.result = '合格'
        }
      }
      this.quality = JSON.parse(JSON.stringify(this.quality))
    },
    check() {
      for (const row of this.quality.entitys) {
        if (row.type === '数值') {
          row.judge = '合格'
          if (row.qc >= 1 && row.no1 && (row.miniVal > row.no1 || row.no1 > row.maxVal)) {
            row.result = row.judge = '不合格'
            continue
          }
          if (row.qc >= 2 && row.no2 && (row.miniVal > row.no2 || row.no2 > row.maxVal)) {
            row.result = row.judge = '不合格'
            continue
          }
          if (row.qc >= 3 && row.no3 && (row.miniVal > row.no3 || row.no3 > row.maxVal)) {
            row.result = row.judge = '不合格'
            continue
          }
          if (row.qc >= 4 && row.no4 && (row.miniVal > row.no4 || row.no4 > row.maxVal)) {
            row.result = row.judge = '不合格'
            continue
          }
          if (row.qc >= 5 && row.no5 && (row.miniVa > row.no5 || row.no5 > row.maxVal)) {
            row.result = row.judge = '不合格'
            continue
          }
        }
        row.result = '合格'
      }
      this.quality = JSON.parse(JSON.stringify(this.quality))
    },
    goBack() {
      this.$router.back()
    },
    saveEdit() {
      if (!this.quality.measurement) {
        this.$message.warning('请填写总评')
        return
      }
      this.loading = true
      this.quality.zjStatus = '待审核'
      save(this.quality).then(res => {
        this.loading = false
        this.$message.success(res.message)
        this.$router.push({ path: 'incomeEdit', query: { id: res.data.data.id }})
        if (this.$route.query.id) {
          this.getInCome(this.$route.query.id)
        }
      })
    },
    // 删除操作
    handlefinish(index, row) {
      // 二次确认删除
      this.$confirm('确定要免检吗？', '提示', {
        type: 'warning'
      }).then(() => {
        console.log(row)
        finish(row.AutoID).then(res => {
          this.$message.success('免检成功')
          this.getData()
        })
      })
        .catch(() => {
        })
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, 'pageIndex', 1)
      this.getData()
    },
    getData() {
      this.loading = true
      u8query(this.query).then(res => {
        this.loading = false
        this.page = res.data.page
      })
    },
    handlePageChange(val) {
      this.$set(this.query, 'pageIndex', val)
      this.getData()
    },
    // 分页大小改变
    handleSizeChange(val) {
      this.$set(this.query, 'pageSize', val)
      this.getData()
    },
    selTable(row) {
      this.form = row
      this.editVisible = false
      this.getProject(this.form.cInvCode)
    },
    getMode(level, qty, grade) {
      const data = { level: level, qty: qty, grade: grade }
      get(data).then(res => {
        this.qc = res.data.data.qc
        this.quality.qc = res.data.data.qc
        this.quality.ac = res.data.data.ac
        this.quality.re = res.data.data.re
        this.quality = JSON.parse(JSON.stringify(this.quality))
      })
    },
    getProject(cinvcode) {
      getByCInvCode(cinvcode).then(res => {
        if (res.data.data && res.data.data.length > 0) {
          this.projects = res.data.data
          const data = res.data.data[res.data.data.length - 1]
          this.setQuality(data)
        } else {
          this.$message({
            showClose: true,
            message: '该产品还没有方案',
            type: 'error'
          })
        }
      })
    },
    setGrade() {
      for (let i = 0; i < this.quality.entitys.length; i++) {
        const item = this.quality.entitys[i]
        const data = { level: this.quality.level, qty: this.quality.qty, grade: item.grade }
        get(data).then(res => {
          item.qc = res.data.data.qc
          item.ac = res.data.data.ac
          item.re = res.data.data.re
          this.quality.entitys[i] = item
          this.quality = JSON.parse(JSON.stringify(this.quality))
        })
      }
    },
    setQuality(data) {
      //this.getMode(data.level, this.form.iQuantity, data.grade)
      this.quality.cvencode = this.form.cVenCode
      this.quality.cvenname = this.form.cVenName
      this.quality.cdepname = this.form.cDepName
      this.quality.cdepcode = this.form.cDepCode
      this.quality.autoid = this.form.AutoID
      this.quality.qty = this.form.iQuantity
      this.quality.ccode = this.form.cCode
      this.quality.ddate = this.form.dDate
      this.quality.cwhname = this.form.cWhname
      this.quality.csocode = this.form.cSOCode
      this.quality.csoinvname = this.form.csoinvname
      this.quality.cinvname = data.cinvname
      this.quality.cinvstd = data.cinvstd
      this.quality.cfree1 = data.cFree1
      this.quality.aql = data.aql
      this.quality.projectId = data.id
      this.quality.projectDescribe = data.describe
      this.quality.projectName = data.name
      this.quality.remark = data.remark

      this.quality.level = data.level
      console.log(15)

      if(this.quality.version == data.version && this.quality.cinvcode == data.cinvcode ){
        for (const item of this.quality.entitys) {
          item.id = null
          item.refId  = null
        }
      }else{
        this.quality.cinvcode = data.cinvcode
        this.quality.version = data.version
        let entitys = []
        for (const item of data.entitys) {
          const ent = { quotaId: item.quota.id, quotaName: item.quota.name, type: item.type,
            planId: item.quota.basePlan.id, planName: item.quota.basePlan.name, specs: item.specs,aql:item.aql,
            judge: '合格', result: '合格',grade:item.grade,qc:5,
            pda:0,pdb:0,pdc:0,
            strVal: item.strVal, miniVal: item.miniVal, stanVal: item.stanVal, maxVal: item.maxVal }
          entitys.push(ent)
        }
        this.quality.entitys = entitys
      }

      this.active = 2
      this.setChoujian()
      //this.setGrade()
    },
    setChoujian(){
      if (this.quality.level && this.quality.qty) {
        findDemoByLevelAndQty({ level: this.quality.level, qty: this.quality.qty }).then(res => {
          if (res.code == 200) {
            this.quality.checkqty = res.data.data.ISAMPLEQTY
          }
          this.changeDemo()
        })
      }
    },
    changeDemo() {
      this.changeAqla()
      this.changeAqlb()
      this.changeAqlc()
    },
    selVersion(e) {
      for (const item of this.projects) {
        if (item.id === e) {
          this.setQuality(item)
          break
        }
      }
    },
    toNext() {
      getNext(this.$route.query.id).then(res => {
        this.$router.push({ path: 'incomeEdit', query: { id: res.data.data }})
        if (this.$route.query.id) {
          this.getInCome(this.$route.query.id)
        }
      })
    },
    toLast() {
      getLast(this.$route.query.id).then(res => {
        this.$router.push({ path: 'incomeEdit', query: { id: res.data.data }})
        if (this.$route.query.id) {
          this.getInCome(this.$route.query.id)
        }
      })
    },
    changeAqla(){
      if(!this.quality.checkqty){
        this.$message({
          showClose: true,
          message: '请先选择单据',
          type: 'warning'
        })
        return
      }
      if(this.quality.aqla === 0){
        this.quality.aca = 0
        this.quality.rea = 0
      }else if(this.quality.aqla){
        const data = { level: this.quality.level, qty: this.quality.checkqty, grade: this.quality.aqla }
        get(data).then(res => {
          this.quality.aca = res.data.data.ac
          this.quality.rea = res.data.data.re
          for(const item of this.quality.entitys){
            if(item.aql === 'A'){
              item.aca = res.data.data.ac
              item.rea = res.data.data.re
            }
          }
          this.panduan()
        })
      }
    },
    changeAqlb(){
      if(!this.quality.checkqty){
        this.$message({
          showClose: true,
          message: '请先选择单据',
          type: 'warning'
        })
        return
      }
      if(this.quality.aqlb === 0){
        this.quality.acb = 0
        this.quality.reb = 0
      }else if(this.quality.aqlb){
        const data = { level: this.quality.level, qty: this.quality.checkqty, grade: this.quality.aqlb }
        get(data).then(res => {
          this.quality.acb = res.data.data.ac
          this.quality.reb = res.data.data.re
          for(const item of this.quality.entitys){
            if(item.aql === 'B'){
              item.acb = res.data.data.ac
              item.reb = res.data.data.re
            }
          }
          this.panduan()
        })
      }
    },
    changeAqlc(){
      if(!this.quality.checkqty){
        this.$message({
          showClose: true,
          message: '请先选择单据',
          type: 'warning'
        })
        return
      }
      if(this.quality.aqlc === 0){
        this.quality.acc = 0
        this.quality.rec = 0
      }else if(this.quality.aqlc){
        const data = { level: this.quality.level, qty: this.quality.checkqty, grade: this.quality.aqlc }
        get(data).then(res => {
          this.quality.acc = res.data.data.ac
          this.quality.rec = res.data.data.re
          for(const item of this.quality.entitys){
            if(item.aql === 'C'){
              item.acc = res.data.data.ac
              item.rec = res.data.data.re
            }
          }
          this.panduan()
        })
      }
    },
    add() {
      this.$router.push({ path: 'incomeEdit' })
    },
    clearQuery() {
      this.query.ccode = null
      this.query.cvenname = null
      this.query.sddate = null
      this.query.eddate = null
    },
  }
}
</script>

<style scoped>

  .handle-input {
    margin: 5px;
    width: 300px;
    display: inline-block;
  }
  .table {
    width: 100%;
    font-size: 14px;
  }
  .red {
    color: #ff0000;
  }
  .hand-btn {
    margin: 5px;
  }
</style>
